import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
import { ValidationPipe } from '@nestjs/common';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  // 获取 ConfigService 实例
  const configService = app.get(ConfigService);

  // 读取端口（支持默认值）
  const port = configService.get<number>('APP_PORT', 3000);

  // 启用 DTO 校验
  // app.useGlobalPipes(
  //   new ValidationPipe({
  //     whitelist: true,
  //     forbidNonWhitelisted: true,
  //     exceptionFactory: (errors) => {
  //       console.log(errors);
  //       const a = errors.map((e) => {
  //         const rule = Object.keys(e.constraints!)[0];
  //         const msg = e.constraints![rule];
  //         return msg;
  //       })[0];
  //       console.log('a', a);
  //     },
  //   }),
  // );
  await app.listen(port);

  return port;
}

bootstrap()
  .then((port) => {
    console.log(`Server started on port ${port ?? 3000}`);
  })
  .catch((error) => {
    console.log('Server failed to start', error);
  });
